package com.lance.shiro.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RequestUtils {
    public Logger log = LogManager.getLogger(getClass());
    public static ResponseEntity success(String msg) {
        return success(msg, null);
    }

    public static ResponseEntity success(String msg, Object data) {
        return result(true, msg, data);
    }

    public static ResponseEntity error(String msg) {
        return error(msg, null);
    }

    public static ResponseEntity error(String msg, Object data) {
        return result(false, msg, data);
    }

    public static ResponseEntity result(Boolean success, String msg, Object data) {
        HashMap<String, Object> re = new HashMap<>();
        re.put("success", success);
        re.put("msg", msg);
        re.put("data", data);
        return success? ResponseEntity.ok(re) :ResponseEntity.badRequest().body(re);
    }


    @ExceptionHandler(value = Exception.class)
    public ResponseEntity defaultErrorHandler(HttpServletRequest req, Exception e)  {
        //打印异常信息：
        log.error(req.getRequestURL());
        log.error(e);
        e.printStackTrace();
        return RequestUtils.error(e.getMessage(),null);
    }
}
